package com.ak.server.platform.controller;

import com.ak.common.annotation.LogEndpoint;
import com.ak.common.controller.BaseController;
import com.ak.common.entity.AKResponse;
import com.ak.common.entity.Enums;
import com.ak.common.entity.system.Tenant;
import com.ak.common.entity.system.TenantApplication;
import com.ak.common.utils.EnumUtil;
import com.ak.common.utils.StringUtil;
import com.ak.server.platform.service.ITenantApplicationService;
import com.ak.server.platform.service.ITenantService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 租户管理
 */
@Api(tags = "租户模块")
@RestController
@RequestMapping("tenant")
public class TenantController extends BaseController {

    @Autowired
    private ITenantService tenantService;

    @Autowired
    private ITenantApplicationService tenantApplicationService;

    /**
     * 系统管理列表
     *
     * @param tenant
     * @returnt
     */
    @ApiOperation("查询租户")
    @LogEndpoint(operation = "查询租户")
    @GetMapping
    public AKResponse list(Page page, Tenant tenant) {
        tenant.setDelFlag(Enums.DelFlag.EXIST.getCode());
        return new AKResponse().data(tenantService.page(page, Wrappers.query(tenant)));
    }

    @ApiOperation("查询所有租户")
    @LogEndpoint(operation = "查询所有租户")
    @GetMapping("getAllTenant")
    public AKResponse getAllTenant(Tenant tenant) {
        tenant.setDelFlag(Enums.DelFlag.EXIST.getCode());
        return new AKResponse().data(tenantService.selectTenantList(tenant));
    }

    /**
     * @return
     */
    @ApiOperation("获取租户编码")
    @GetMapping("getTenantCode")
    public AKResponse getTenantCode() {
        return new AKResponse().data(StringUtil.random(8, Enums.RandomType.INT));
    }

    /**
     * @param tenant
     * @return
     */
    @ApiOperation("添加租户")
    @LogEndpoint(operation = "添加租户")
    @PostMapping
    public AKResponse add(@Validated Tenant tenant) {
        tenant.setCreateBy(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        tenant.setStatus(Enums.Status.OK.getCode());
        tenant.setCreateTime(new Date());
        return new AKResponse().data(tenantService.save(tenant));
    }

    @ApiOperation("更新租户")
    @LogEndpoint(operation = "更新租户")
    @PutMapping
    public AKResponse update(@Validated Tenant tenant) {
        tenant.setUpdateBy(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        tenant.setUpdateTime(new Date());
        return new AKResponse().data(tenantService.updateById(tenant));
    }

    /**
     * 校验编码
     */
    @GetMapping("/checkCodeExist/{tenantCode}")
    @ResponseBody
    public boolean checkCodeExist(@PathVariable("tenantCode") String tenantCode) {
        return tenantService.selectTenantByTenantCode(tenantCode) != null ? true : false;
    }

    /**
     * @param id
     * @return
     */
    @ApiOperation("删除租户")
    @LogEndpoint(operation = "删除租户")
    @DeleteMapping("/{id}")
    public AKResponse delete(@PathVariable("id") String id) {
        Tenant tenant = new Tenant();
        tenant.setTenantId(id).setDelFlag(Enums.DelFlag.DEL.getCode());
        return new AKResponse().data(tenantService.updateById(tenant));
    }

    @ApiOperation("获取用于分配的子系统")
    @GetMapping("/getAuthApplications/{tenantCode}")
    public AKResponse getAuthApplications(@PathVariable("tenantCode") String tenantCode) {
        JSONObject result = new JSONObject();

        // 所有的
        JSONArray all = EnumUtil.getEnumToJSONArray(Enums.ApplicationCodes.class);
        // 已分配的
        JSONArray exist = new JSONArray();
        exist.addAll(tenantApplicationService.list(Wrappers.query(new TenantApplication().setTenantCode(tenantCode))));

        result.put("all", all);
        result.put("exist", exist);
        return new AKResponse().data(result);
    }

    @ApiOperation("分配子系统")
    @PutMapping("/authApplication")
    @ResponseBody
    public boolean authApplication(String tenantCode, String appCodes) {
        return tenantApplicationService.authApplication(tenantCode, appCodes);
    }
}
